package domain;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import dal.ConnectionManager;

public class Activity {

	/** id of activity */
	private Integer id;
	/** name of the activity */
	private String name;
	/** description of the activity */
	private String desc;
	/** starting date of the activity */
	private Date startDate = null;
	/** the total hours of working for activity */
	private Integer hours;
	/** the deliverable of the activity */
	private String deliverable;
	/** the project which the activity connects */
	private Integer projectId;
    /** the project which the activity connects */
	private Project myProject;
	/** This is used to converting the date yyyy-MM-dd format */
	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
	/** These instances are needed for database connection */
	ConnectionManager mgr = ConnectionManager.getInstance();
	Connection con = mgr.getConnection();
	
	/** Empty constructor of the Activity class */
	public Activity() {
		super();
	}

	/**
	 * Constructor of the Activity class
	 *                           
	 * @param name Name of the activity
	 * @param desc Description of the project
	 * @param dateInput Starting date of the activity
	 * @param hours Total working hours of the activity
	 * @param deliverable Deliverable of the activity
	 * @param projectId Project id of the project which the activity connects 
	 */
	public Activity(String name, String desc, String dateInput, Integer hours,String deliverable, Integer projectId) {
		super();
		this.name = name;
		this.desc = desc;
		setStartDate(dateInput);
		this.hours = hours;
		this.deliverable = deliverable;
		this.projectId = projectId;
		
	}

	/**
	 * The method for creating an activity
	 * 
	 * It calls the task create method because an activity can not be
	 * create without tasks
	 *                            
	 * @param myProject Object of the project
	 * @param myActivity Object of the Activity
	 * @param myTask Object of the Task
	 * @return void 
	 */
	public boolean create(Project myProject, Activity myActivity, Task myTask)
	{
		if(myActivity.selectById(myActivity.getId())==null)
		{
		  if(myActivity.getStartDate().before(myProject.getStartDate()))
		   {
			return false;
		   }
		  else{
		    myActivity.setProjectId(myProject.getId());
		    myActivity.addActivity(myActivity);
	     	}
		}
		if(myTask.create(myActivity, myTask))
		{
			return true;
		}
		return false;
		}
	

	/**
	 * Adding an activity to database
	 * @param myActivity
	 */
	public void addActivity(Activity myActivity)
    {
    	
		try {
			Statement st = con.createStatement();
			st.executeUpdate("insert into activity (name, description, startDate, hours, deliverable, projectId) values ('"+myActivity.getName()+"', '"+myActivity.getDesc()+"', '"+myActivity.getDateToString()+"', "+myActivity.getHours()+", '"+myActivity.getDeliverable() +"', "+myActivity.getProjectId()+")");
			ResultSet rs = st.executeQuery("select id from activity where name='"+ myActivity.getName() +"' and description='"+ myActivity.getDesc() +"' and startDate='"+ myActivity.getDateToString()+"'");
			while (rs.next()) {
				myActivity.setId(rs.getInt(1));	
			}	
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
    }
	
	/**
	 * Removing an activity form database
	 * @param myActivity
	 */
	public void removeActivity(Activity myActivity)
    {
		try {
			Statement st = con.createStatement();
			st.executeUpdate("delete from activity where id="+ myActivity.getId());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
	
	/**
	 * Updating an activity
	 * @param myActivity
	 */
	public void updateActivity(Activity myActivity)
	{
	    try {
			Statement st = con.createStatement();
			st.executeUpdate("update activity set name='"+ myActivity.getName() +"', description='"+ myActivity.getDesc() +"', startDate='"+ myActivity.getDateToString() +"', hours="+ myActivity.getHours() +", deliverable='"+ myActivity.getDeliverable() +"', projectId="+ myActivity.getProjectId()+" where id="+myActivity.getId());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * Finding the activity that given its id
	 * @param id 
	 * @return
	 */
	public Activity selectById(Integer id) 
	{
		try {
			Statement st = con.createStatement();
			ResultSet rs = st.executeQuery("select * from activity where id="+id);
			Activity myActivity = new Activity();
			if (rs.next()) {
			     do {
			    	   
						myActivity.setId(rs.getInt(1));
						myActivity.setName(rs.getString(2));
						myActivity.setDesc(rs.getString(3));
						myActivity.setStartDate(rs.getString(4));
						myActivity.setHours(rs.getInt(5));
						myActivity.setDeliverable(rs.getString(6));
						myActivity.setProjectId(rs.getInt(7));
			       } while (rs.next());
			     
			 	return myActivity;  
			     }
			  else {
			    return null;
		}			
	
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * Finding the activities which connects the given project
	 * @param myProject
	 * @return
	 */
	public ArrayList <Activity> selectbyProject(Project myProject) 
	{
		try {
			Statement st = con.createStatement();
			ResultSet rs = st.executeQuery("select * from activity where projectId="+myProject.getId());
			ArrayList <Activity> myActivityList = new ArrayList <Activity> ();
			if (rs.next()) {
			     do {
			    	    Activity myActivity = new Activity();
						myActivity.setId(rs.getInt(1));
						myActivity.setName(rs.getString(2));
						myActivity.setDesc(rs.getString(3));
						myActivity.setStartDate(rs.getString(4));
						myActivity.setHours(rs.getInt(5));
						myActivity.setDeliverable(rs.getString(6));
						myActivity.setProjectId(rs.getInt(7));
						myActivityList.add(myActivity);
			       } while (rs.next());
			 	return myActivityList;  
			     }
			  else {
			    return null;
		}			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * Printing the activities which connects the given project on console
	 * @param myProject
	 */
	public void writeSelectByProject(Project myProject) 
	{
		try {
			Statement st = con.createStatement();
			ResultSet rs = st.executeQuery("select * from activity where projectId="+ myProject.getId());
			while(rs.next())
			{
			   System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3)+" "+rs.getString(4)+" "+rs.getInt(5)+" "+rs.getString(6)+" "+rs.getInt(7));	
			}
						
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		}
	}
	
	/**
	 * Getters and setters
	 * @return
	 */
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	public Date getStartDate() {
		return startDate;
	}

	public String getDateToString()
	{
		return (format.format(startDate));
	}
	
    public void setStartDate(String dateInput) {
		
        Project myPrjt = new Project();
		
		if(myPrjt.selectById(projectId)!=null)
		{
			if(startDate.before(myPrjt.selectById(projectId).getStartDate()))
			{
				System.out.println("THE ACTIVITY STARTDATE CAN NOT BE BEFORE THE PROJECT STARTDATE!!!");
			}
			else
			{
				try {  
					   startDate = format.parse(dateInput);
				    } catch (ParseException e) {
				    	e.printStackTrace();
				    }
			}
		}
		
		else{
			if(dateInput==null)
	    	{
	    	   startDate = null;	
	    	}
			else{
				
				try {  
					   startDate = format.parse(dateInput);
				    } catch (ParseException e) {
				    	e.printStackTrace();
				    }
			}
			
		}
    	
	}

	public Integer getHours() {
		return hours;
	}

	public void setHours(Integer hourInput) {
		if(hours==null)
		{
			hours=hourInput;
		}
		else{
		hours = hours + hourInput;
		}
	}

	public String getDeliverable() {
		return deliverable;
	}

	public void setDeliverable(String deliverable) {
		this.deliverable = deliverable;
	}

	public Integer getProjectId() {
		return projectId;
	}

	public void setProjectId(Integer projectId) {
		this.projectId = projectId;
	}

	public Project getMyProject() {
		return myProject;
	}

	public void setMyProject(Project myProject) {
		this.myProject = myProject;
	}
	
}
